import sys
import os
import re

"""
顺时针输出二维数组的值
"""
class Solution:
    def circlePrint(self, input_):
        # 数组保存值
        array2d = []
        for arr in input_:
            array1d = []
            for num in arr:
                array1d.append(num)
            array2d.append(array1d)

        col=len(array1d)
        row=len(array2d)

        u, v = 0, col
        k, t = 0, row

        result_list = []
        # Write Code Here 
        while u < v or k < t:
            # 首行从左往右
            for i in range(u, v):
                result_list.append(array2d[k][i])
            print("reslist", result_list)

            # 末列从上往下
            for i in range(k+1, t):
                result_list.append(array2d[i][v-1])
            print("2: ",result_list)

            # 末行从右往左
            for i in range(v-2, u-1, -1):
                result_list.append(array2d[t-1][i])
            print("3: ", result_list)

            # 首列从下到上
            for i in range(t-2, k, -1):
                result_list.append(array2d[i][u])
            u+=1
            k+=1
            t-=1
            v-=1

        return result_list

_input_rows = 0
_input_cols = 0
_input_rows = int(input())
_input_cols = int(input())

_input = []
for _input_i in range(_input_rows):
    _input_temp = map(int,re.split(r'\s+', input().strip()))
    _input.append(_input_temp)

s = Solution()
res = s.circlePrint(_input)

for res_cur in res:
    print(str(res_cur) + "\n")